package com.example.administrator.szb.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;

import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BaseActivity;
import com.example.administrator.szb.fragments.fragment_forXM.FragmentXM2;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * Created by D N on 2017/10/30.
 */

public class XMGLActivity extends BaseActivity {
    @Bind(R.id.xmgl_radiogroup_xm)
    RadioGroup xmglRadiogroupXm;
    private FragmentManager fragmentManager;
    private ArrayList<Fragment> fragmentArrayList;
    private FragmentXM2 fragment_wfcdxm, fragment_wsddxm;
    private FragmentTransaction fragmentTransaction;
    private Bundle bundle = new Bundle();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xmgl_activity);
        ButterKnife.bind(this);
        initToolbar(R.id.xmgl_toolbar_include,"项目管理");
        initView();
    }

    private void initView() {
        initFragment();//开启问题类型的切换
        xmglRadiogroupXm.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i){
                    case R.id.xmgl_radiobutton_wfcdxm:
                        changeStatu(0);
                        break;
                    case R.id.xmgl_radiobutton_wsddxm:
                        changeStatu(1);
                        break;
                }
            }
        });
    }

    private void initFragment() {
        fragmentManager = getSupportFragmentManager();
        fragmentArrayList = new ArrayList<Fragment>();
        changeStatu(0);
    }
    /**
     * 根据点击问题的不同类型修改字体颜色
     *
     * @param i
     */
    private void changeStatu(int i) {
        fragmentTransaction = fragmentManager.beginTransaction();
        hideFragments(fragmentTransaction);
        switch (i) {
            case 0:
                if(fragment_wfcdxm == null){
                    fragment_wfcdxm = new FragmentXM2();
                    bundle.putInt("type",1);
                    fragment_wfcdxm.setArguments(bundle);
                    changeFragment(fragment_wfcdxm);
                }else {
                    fragmentTransaction.show(fragment_wfcdxm);
                }

                break;
            case 1:
                if(fragment_wsddxm == null){
                    fragment_wsddxm = new FragmentXM2();
                    bundle.putInt("type",2);
                    fragment_wsddxm.setArguments(bundle);
                    changeFragment(fragment_wsddxm);
                }else {
                    fragmentTransaction.show(fragment_wsddxm);
                }
                break;
        }

        fragmentTransaction.commit();
    }

    private void changeFragment(Fragment fragment) {
        fragmentArrayList.add(fragment);
        fragmentTransaction.add(R.id.xmgl_content, fragment);
        fragmentTransaction.show(fragment);
    }

    private void hideFragments(FragmentTransaction fragmentTransaction) {
        for (Fragment fragment : fragmentArrayList) {
            fragmentTransaction.hide(fragment);
        }
    }
}
